package org.duang.config;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.AsyncContext;
import javax.servlet.ServletContext;

import org.duang.aop.Interceptor;
import org.duang.aop.Proxy;
import org.duang.core.Action;
import org.duang.core.Controller;
import org.duang.handles.IHandle;
import org.duang.ioc.Import;
import org.duang.plugins.IPlugin;

import com.esotericsoftware.reflectasm.MethodAccess;


/**
 * 实例工厂
 * @author laotang
 */
public class InstanceFactory {
	
	private static ServletContext servletContext;

	private static Map<String, Class<? extends Controller>> routesMaps;
	
	private static ConcurrentMap<String, MethodAccess> methodAccessMaps = new ConcurrentHashMap<String, MethodAccess>();
	
	private static Map<String, Action> actionMapping;
	
	private static List<IHandle> handles = new ArrayList<IHandle>();
	
	private static List<IPlugin> pluginList = new ArrayList<IPlugin>();
	
	private static List<Interceptor> inters = new ArrayList<Interceptor>();
	
	private static ConcurrentHashMap<Class<? extends Annotation>,Class<? extends Proxy>> annotAtionMap = new ConcurrentHashMap<Class<? extends Annotation>,Class<? extends Proxy>>();
	
	private static List<Class<? extends Annotation>> annotImportList = new ArrayList<Class<? extends Annotation>>();
	
	private static ConcurrentHashMap<Class<?>,List<Proxy>> proxyServiceMap = new ConcurrentHashMap<Class<?>,List<Proxy>>();
	
	private static ConcurrentLinkedQueue<AsyncContext> asyncContextQueue;
	
	private static Map<String, Map<String,String>> drlUriMap =  Collections.synchronizedMap(new LinkedHashMap<String,Map<String,String>>());
	
	
	/**
	 * 取得Action处理器
	 * @return
	 */
	public static List<IHandle> getHandles() {
		return InstanceFactory.handles;
	}

	public static void setHandles(IHandle handle) {
		InstanceFactory.handles.add(handle);
	}
	
	public static void setInterceptor(Interceptor interceptor) {
		InstanceFactory.inters.add(interceptor);
	}
	
	public static Interceptor[] getInterceptor() {
		return InstanceFactory.inters.toArray(new Interceptor[InstanceFactory.inters.size()]);
	}

	/**
	 * 取得Controller映射集合
	 * @return
	 */
	public static Map<String, Action> getActionMapping() {
		return actionMapping;
	}
	
	/**
	 * action map集合
	 * @param actionMapping
	 */
	public static void setActionMapping(Map<String, Action> actionMapping) {
		InstanceFactory.actionMapping = actionMapping;
	}
	

	/**
	 * 取得Route集合
	 * @return
	 */
	public static Map<String, Class<? extends Controller>> getRoutesMaps() {
		return routesMaps;
	}

	public static void setRoutesMaps(Map<String, Class<? extends Controller>> routesMaps) {
		InstanceFactory.routesMaps = routesMaps;
	}
	
	/**
	 * 取得ServletContext
	 * @return
	 */
	public static ServletContext getServletContext() {
		return servletContext;
	}

	public static void ServletContext(ServletContext servletContext) {
		InstanceFactory.servletContext = servletContext;
	}

	public static List<IPlugin> getPluginList() {
		return pluginList;
	}
	
	public static void setPlugin(IPlugin plugin) {
		InstanceFactory.pluginList.add(plugin);
	}

	public static void setPluginList(List<IPlugin> pluginList) {
		InstanceFactory.pluginList = pluginList;
	}

	public static ConcurrentMap<String, MethodAccess> getMethodAccessMaps() {
		return methodAccessMaps;
	}

	public static void setMethodAccessMaps(
			ConcurrentMap<String, MethodAccess> methodAccessMaps) {
		InstanceFactory.methodAccessMaps = methodAccessMaps;
	}

	/**
	 * 推送队列
	 * @return
	 */
	public static ConcurrentLinkedQueue<AsyncContext> getAsyncContextQueue() {
		return InstanceFactory.asyncContextQueue;
	}

	public static void initAsyncContextQueue() {
		InstanceFactory.asyncContextQueue = new ConcurrentLinkedQueue<AsyncContext>(); 
	}
	
	public static void setAsyncContext2Queue(AsyncContext asyncContext) {
		InstanceFactory.asyncContextQueue.add(asyncContext);
	}
	
	public static void setAnnotation( Map<Class<? extends Annotation>,Class<? extends Proxy>> map ) {
		InstanceFactory.annotAtionMap.putAll(map);
	}
	
	public static Map<Class<? extends Annotation>,Class<? extends Proxy>> getAnnotation() {
		return InstanceFactory.annotAtionMap;
	}
	public static void setProxyService(Class<?> clazz,  List<Proxy> proxyList ) {
		InstanceFactory.proxyServiceMap.put(clazz,proxyList);
	}
	
	public static Map<Class<?>,List<Proxy>> getProxyService() {
		return InstanceFactory.proxyServiceMap;
	}

	//请求线程集合
	public static Map<String, Map<String,String>> getDrlUriMap() {
		return drlUriMap;
	}

	
	public static void setImportAnnot(Class<? extends Annotation> clazz) {
		InstanceFactory.annotImportList.add(clazz);
	}
	
	public static List<Class<? extends Annotation>> getImportAnnot() {
		return InstanceFactory.annotImportList;
	}
}
